package com.example.whatrubbish.controller;

import com.example.whatrubbish.model.LoginRecord;
import com.example.whatrubbish.service.LoginRecordService;
import com.example.whatrubbish.utils.Constants;
import com.example.whatrubbish.utils.JsonResult;
import com.example.whatrubbish.vo.Criteria;
import com.example.whatrubbish.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("/admin/loginRecord")
public class LoginRecordController {
    @Autowired
    LoginRecordService loginRecordService;

    @ResponseBody
    @RequestMapping("/last")
    public JsonResult lastRecord(Long loginAdminId){
        JsonResult jsonResult;
        Map map = new HashMap();
        try{
            LoginRecord loginRecord = loginRecordService.selectLastLoginRecord(loginAdminId);
            if(loginRecord == null){
                map.put("ip", "首次登录");
                map.put("addr", "未知");
                map.put("time", "未知");
                map.put("isp", "未知");
            }else{
                map.put("ip", loginRecord.getLoginIp());
                map.put("addr", loginRecord.getLoginCountry()+" "+loginRecord.getLoginRegion()+" "+loginRecord.getLoginCity());
                map.put("time", loginRecord.getLoginTime());
                map.put("isp", loginRecord.getLoginIsp());
            }
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "success", map);
        }catch (Exception e){
            map.put("ip", "登录异常");
            map.put("addr", "未知");
            map.put("time", "未知");
            map.put("isp", "未知");
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "error", map);
        }
        return jsonResult;
    }

    @ResponseBody
    @RequestMapping("/all")
    public JsonResult allRecord(HttpServletRequest request){
        JsonResult jsonResult;
        try{
            Integer pageIndex = request.getParameter("pageIndex")!=null?request.getParameter("pageIndex")!=""? Integer.parseInt(request.getParameter("pageIndex")) : 1 : 1;
            Integer pageSize = request.getParameter("pageSize")!=null? request.getParameter("pageSize")!=""? Integer.parseInt(request.getParameter("pageSize")) : 10 : 10;
            if(request.getParameter("adminId") == null || request.getParameter("adminId") == ""){
                 throw new Exception("啊哦~~发生异常咯");
            }
            String adminId = request.getParameter("adminId");
            PageBean pageBean = new PageBean(pageIndex, pageSize);
            Criteria criteria = new Criteria();
            criteria.addExtField("pageIndex", (pageIndex-1) * pageSize);
            criteria.addExtField("pageSize", pageSize);
            criteria.addExtField("adminId", adminId);
            Long count = loginRecordService.selectAllCountByAdminId(criteria);
            List<LoginRecord> list = loginRecordService.selectAllByAdminId(criteria);
            pageBean.setTotalCount(count);
            pageBean.setResultList(list);
            if(list.size() > 0){
                jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "success", pageBean);
            }else {
                jsonResult = new JsonResult(Constants.STATUS_NOT_FOUND, "fail", pageBean);
            }
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, e.getMessage());
        }
        return jsonResult;
    }
}
